package com.qqt.csr.ixport.controller;

import com.qqt.csr.common.idempotent.Idempotent;
import com.qqt.csr.common.interceptor.auth.Authorize;
import com.qqt.csr.common.log.Log;
import com.qqt.csr.common.operalog.BusinessType;
import com.qqt.csr.common.operalog.LogLevel;
import com.qqt.csr.common.operalog.OperationLog;
import com.qqt.csr.common.session.SessionContextHolder;
import com.qqt.csr.common.vo.resp.ObjectResponse;
import com.qqt.csr.ixport.enums.TaskType;
import com.qqt.csr.ixport.service.BatchImportTaskService;
import com.qqt.csr.ixport.vo.req.CreatingImportTaskReqVO;
import com.qqt.csr.ixport.vo.req.QueryingImportTaskReqVO;
import com.qqt.csr.ixport.vo.req.SwitchingImportTaskStatusReqVO;
import com.qqt.csr.ixport.vo.resp.ImportTaskInfoRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: yang hao
 * @Date: 2025/1/20 10:11
 * @Description: 批量导入任务 控制器
 **/
@Authorize
@RestController
@RequestMapping("/import")
@RequiredArgsConstructor
@SuppressWarnings("all")
@Tag(name = "导入", description = "导入")
public class ImportController {

    /**
     * 批量导入任务服务
     */
    private final BatchImportTaskService batchImportTaskService;

    @PostMapping("/create")
    @Operation(summary = "创建批量导入任务")
    @Log(desc = "创建批量导入任务")
    @Idempotent(model = "import:create")
    @OperationLog(level = LogLevel.HIGH, operationMenu = "创建批量导入任务", buttonName = "创建批量导入任务", businessType = BusinessType.INSERT)
    public ObjectResponse<ImportTaskInfoRespVO> createTask(@RequestBody @Validated CreatingImportTaskReqVO reqVO) {
        String tenantId = SessionContextHolder.getHeaderInfo().getTenantId();
        ImportTaskInfoRespVO respVO = batchImportTaskService.createTask(tenantId, reqVO);
        return ObjectResponse.success(respVO);
    }

    @PostMapping("/list")
    @Operation(summary = "获取任务列表")
    public ObjectResponse<List<ImportTaskInfoRespVO>> list(@RequestBody QueryingImportTaskReqVO reqVO) {
        if(reqVO == null){
            reqVO = new QueryingImportTaskReqVO();
        }
        String tenantId = SessionContextHolder.getHeaderInfo().getTenantId();
        Long creatorId = SessionContextHolder.getHeaderInfo().getUserId();
        List<ImportTaskInfoRespVO> list = batchImportTaskService.list(tenantId, creatorId, reqVO);
        return ObjectResponse.success(list);
    }

    @GetMapping("/detail/{id}")
    @Operation(summary = "获取任务信息")
    public ObjectResponse<ImportTaskInfoRespVO> detail(@NotNull(message = "id不能为空") @PathVariable("id") Long id) {
        String tenantId = SessionContextHolder.getHeaderInfo().getTenantId();
        Long creatorId = SessionContextHolder.getHeaderInfo().getUserId();
        ImportTaskInfoRespVO detail = batchImportTaskService.getTaskInfo(tenantId, creatorId, id);
        return ObjectResponse.success(detail);
    }

    @PostMapping("/switch")
    @Log(desc = "切换导入任务状态")
    @Operation(summary = "切换导入任务状态")
    @Idempotent(model = "import:switch")
    @OperationLog(level = LogLevel.HIGH, operationMenu = "切换导入任务状态", buttonName = "切换导入任务状态", businessType = BusinessType.UPDATE)
    public ObjectResponse<String> switchTaskStatus(@RequestBody @Validated SwitchingImportTaskStatusReqVO reqVO) {
        String tenantId = SessionContextHolder.getHeaderInfo().getTenantId();
        Long creatorId = SessionContextHolder.getHeaderInfo().getUserId();
        batchImportTaskService.switchTaskStatus(tenantId, creatorId, reqVO);
        return ObjectResponse.success();
    }

}
